// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie;

import java.util.ArrayList;
import zombie.Lua.LuaEventManager;
import zombie.characters.IsoPlayer;
import zombie.core.Rand;
import zombie.debug.DebugLog;
import zombie.debug.DebugType;
import zombie.iso.IsoWorld;
import zombie.iso.RoomDef;
import zombie.iso.Vector2;
import zombie.network.GameServer;

public final class AmbientSoundManager extends zombie.BaseAmbientStreamManager {
    public final ArrayList<zombie.AmbientSoundManager.Ambient> ambient = new ArrayList();
    private final Vector2 tempo = new Vector2();
    private int electricityShutOffState = -1;
    private long electricityShutOffTime = 0L;
    public boolean initialized = false;

    @Override
    public void update() {
        if (this.initialized) {
            this.updatePowerSupply();
            this.doOneShotAmbients();
        }
    }

    @Override
    public void addAmbient(String name, int x, int y, int radius, float volume) {
    }

    @Override
    public void addAmbientEmitter(float x, float y, int z, String name) {
    }

    @Override
    public void addDaytimeAmbientEmitter(float x, float y, int z, String name) {
    }

    @Override
    public void doOneShotAmbients() {
        for (int _int = 0; _int < this.ambient.size(); _int++) {
            zombie.AmbientSoundManager.Ambient ambientx = (zombie.AmbientSoundManager.Ambient)this.ambient.get(_int);
            if (ambientx.finished()) {
                DebugLog.log(DebugType.Sound, "ambient: removing ambient sound " + ambientx.name);
                this.ambient.remove(_int--);
            } else {
                ambientx.update();
            }
        }
    }

    @Override
    public void init() {
        if (!this.initialized) {
            this.initialized = true;
        }
    }

    @Override
    public void addBlend(String name, float vol, boolean bIndoors, boolean bRain, boolean bNight, boolean bDay) {
    }

    @Override
    protected void addRandomAmbient() {
        if (!GameServer.Players.isEmpty()) {
            IsoPlayer isoPlayer = (IsoPlayer)GameServer.Players.get(Rand.Next(GameServer.Players.size()));
            if (isoPlayer != null) {
                String string = null;
                if (zombie.GameTime.instance.getHour() > 7 && zombie.GameTime.instance.getHour() < 21) {
                    switch (Rand.Next(3)) {
                        case 0:
                            if (Rand.Next(10) < 2) {
                                string = "MetaDogBark";
                            }
                            break;
                        case 1:
                            if (Rand.Next(10) < 3) {
                                string = "MetaScream";
                            }
                    }
                } else {
                    switch (Rand.Next(5)) {
                        case 0:
                            if (Rand.Next(10) < 2) {
                                string = "MetaDogBark";
                            }
                            break;
                        case 1:
                            if (Rand.Next(13) < 3) {
                                string = "MetaScream";
                            }
                            break;
                        case 2:
                            string = "MetaOwl";
                            break;
                        case 3:
                            string = "MetaWolfHowl";
                    }
                }

                if (string != null) {
                    float float0 = isoPlayer.x;
                    float float1 = isoPlayer.y;
                    double _double = (double)Rand.Next((float) -Math.PI, (float) Math.PI);
                    this.tempo.x = (float)Math.cos(_double);
                    this.tempo.y = (float)Math.sin(_double);
                    this.tempo.setLength(1000.0F);
                    float0 += this.tempo.x;
                    float1 += this.tempo.y;
                    zombie.AmbientSoundManager.Ambient ambientx = new zombie.AmbientSoundManager.Ambient(string, float0, float1, 50.0F, Rand.Next(0.2F, 0.5F));
                    this.ambient.add(ambientx);
                    GameServer.sendAmbient(string, (int)float0, (int)float1, 50, Rand.Next(0.2F, 0.5F));
                }
            }
        }
    }

    @Override
    public void doGunEvent() {
        ArrayList arrayList = GameServer.getPlayers();
        if (!arrayList.isEmpty()) {
            IsoPlayer isoPlayer = (IsoPlayer)arrayList.get(Rand.Next(arrayList.size()));
            String string = null;
            float float0 = isoPlayer.x;
            float float1 = isoPlayer.y;
            short _short = 600;
            double _double = (double)Rand.Next((float) -Math.PI, (float) Math.PI);
            this.tempo.x = (float)Math.cos(_double);
            this.tempo.y = (float)Math.sin(_double);
            this.tempo.setLength((float)(_short - 100));
            float0 += this.tempo.x;
            float1 += this.tempo.y;
            zombie.WorldSoundManager.instance.addSound(null, (int)float0 + Rand.Next(-10, 10), (int)float1 + Rand.Next(-10, 10), 0, 600, 600);
            switch (Rand.Next(6)) {
                case 0:
                    string = "MetaAssaultRifle1";
                    break;
                case 1:
                    string = "MetaPistol1";
                    break;
                case 2:
                    string = "MetaShotgun1";
                    break;
                case 3:
                    string = "MetaPistol2";
                    break;
                case 4:
                    string = "MetaPistol3";
                    break;
                case 5:
                    string = "MetaShotgun1";
            }

            float float2 = 1.0F;
            zombie.AmbientSoundManager.Ambient ambientx = new zombie.AmbientSoundManager.Ambient(string, float0, float1, 700.0F, float2);
            this.ambient.add(ambientx);
            GameServer.sendAmbient(string, (int)float0, (int)float1, (int)Math.ceil((double)ambientx.radius), ambientx.volume);
        }
    }

    @Override
    public void doAlarm(RoomDef room) {
        if (room != null && room.building != null && room.building.bAlarmed) {
            float _float = 1.0F;
            zombie.AmbientSoundManager.Ambient ambientx = new zombie.AmbientSoundManager.Ambient(
                "burglar2", (float)(room.x + room.getW() / 2), (float)(room.y + room.getH() / 2), 700.0F, _float
            );
            ambientx.duration = 49;
            ambientx.worldSoundDelay = 3;
            room.building.bAlarmed = false;
            room.building.setAllExplored(true);
            this.ambient.add(ambientx);
            GameServer.sendAlarm(room.x + room.getW() / 2, room.y + room.getH() / 2);
        }
    }

    @Override
    public void stop() {
        this.ambient.clear();
        this.initialized = false;
    }

    private void updatePowerSupply() {
        boolean _boolean = zombie.GameTime.getInstance().NightsSurvived < zombie.SandboxOptions.getInstance().getElecShutModifier();
        if (this.electricityShutOffState == -1) {
            IsoWorld.instance.setHydroPowerOn(_boolean);
        }

        if (this.electricityShutOffState == 0) {
            if (_boolean) {
                IsoWorld.instance.setHydroPowerOn(true);
                this.checkHaveElectricity();
                this.electricityShutOffTime = 0L;
            } else if (this.electricityShutOffTime != 0L && System.currentTimeMillis() >= this.electricityShutOffTime) {
                this.electricityShutOffTime = 0L;
                IsoWorld.instance.setHydroPowerOn(false);
                this.checkHaveElectricity();
            }
        }

        if (this.electricityShutOffState == 1 && !_boolean) {
            this.electricityShutOffTime = System.currentTimeMillis() + 2650L;
        }

        this.electricityShutOffState = _boolean ? 1 : 0;
    }

    private void checkHaveElectricity() {
    }

    public class Ambient {
        public float x;
        public float y;
        public String name;
        public float radius;
        public float volume;
        long startTime;
        public int duration;
        public int worldSoundDelay = 0;

        public Ambient(String string, float float0, float float1, float float2, float float3) {
            this.name = string;
            this.x = float0;
            this.y = float1;
            this.radius = float2;
            this.volume = float3;
            this.startTime = System.currentTimeMillis() / 1000L;
            this.duration = 2;
            this.update();
            LuaEventManager.triggerEvent("OnAmbientSound", string, float0, float1);
        }

        public boolean finished() {
            long _long = System.currentTimeMillis() / 1000L;
            return _long - this.startTime >= (long)this.duration;
        }

        public void update() {
            long _long = System.currentTimeMillis() / 1000L;
            if (_long - this.startTime >= (long)this.worldSoundDelay) {
                zombie.WorldSoundManager.instance.addSound(null, (int)this.x, (int)this.y, 0, 600, 600);
            }
        }
    }
}
